
package com.joysuch.wwyt.bp.service.impl;

import com.joysuch.wwyt.bp.bean.BpCertificateAddBean;
import com.joysuch.wwyt.bp.bean.BpCertificateAddListBean;
import com.joysuch.wwyt.bp.bean.BpCertificateListBean;
import com.joysuch.wwyt.bp.entity.BpCertificate;
import com.joysuch.wwyt.bp.entity.dsl.QBpCertificate;
import com.joysuch.wwyt.bp.repository.BpCertificateDao;
import com.joysuch.wwyt.bp.service.BpCertificateService;
import com.joysuch.wwyt.core.bean.BaseAttachmentBean;
import com.joysuch.wwyt.core.entity.BaseTypeGroupItem;
import com.joysuch.wwyt.core.repository.BaseTypeGroupItemDao;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class BpCertificateServiceImpl implements BpCertificateService {

    @Autowired
    private BpCertificateDao dao;
    @Autowired
    private BaseTypeGroupItemDao typeGroupItemDao;

    @Override
    public List<BpCertificateListBean> getPage(Long userId) {
        QBpCertificate bpCertificate = QBpCertificate.bpCertificate;
        Predicate predicate = bpCertificate.isNotNull();
        predicate = userId == null ? predicate : ExpressionUtils.and(predicate, bpCertificate.usetId.eq(userId));
        Iterable<BpCertificate> all = dao.findAll(predicate);
        ArrayList<BpCertificateListBean> list = new ArrayList<>();
        for (BpCertificate certificate : all) {
            BpCertificateListBean b = new BpCertificateListBean();
            b.setId(certificate.getId());
            b.setCardNumber(certificate.getCardNumber());
            b.setGroupId(certificate.getGroupId());
            BaseTypeGroupItem one = typeGroupItemDao.getOne(certificate.getGroupId());
            b.setGroupName(one.getName());
            b.setUsetId(certificate.getUsetId());
            if (certificate.getValidityEnd() != null) {
                if (certificate.getValidityEnd().after(new Date())) {
                    b.setState("有效");
                } else {
                    b.setState("无效");
                }
            } else {
                b.setState("有效");
            }
            b.setContent("暂时未知");
            list.add(b);
        }
        return list;
    }

    @Override
    public BpCertificateListBean add(BpCertificateAddBean c) throws ParseException {
        BpCertificate b = new BpCertificate();

        // 通过代码查询编码
        BaseTypeGroupItem certType = typeGroupItemDao.findFirstByCode(c.getGroupCode());
        Assert.notNull(certType, "不识别的证件类型:" + c.getGroupCode());

        b.setGroupId(certType.getId());
        if (c.getUserId() != null) {
            List<BpCertificate> list = dao.findByUsetIdAndGroupId(c.getUserId(), certType.getId());
            if (list.size() > 0) {
                throw new IllegalArgumentException("已有同类型证件，不能重复添加");
            }
        }
        b.setUsetId(c.getUserId());
        b.setCardNumber(c.getCardNumber());
        b.setContinuous(c.getContinuous());
        b.setHoldProject(c.getHoldProject());
        b.setRecheckDate(c.getRecheckDate());
        b.setEarlyDate(c.getEarlyDate());
        b.setValidityBegin(c.getValidityBegin());
        b.setValidityEnd(c.getValidityEnd());
        b.setHomeworkId(c.getHomeworkId());
        b.setImageId(c.getImage().getId());
        b.setPostpone(c.getPostpone());
        b.setDownloadurl(c.getImage().getDownloadUrl());
        b.setName(c.getImage().getName());

        BpCertificate certificate = dao.save(b);
        BpCertificateListBean cf = new BpCertificateListBean();
        cf.setId(certificate.getId());
        cf.setCardNumber(certificate.getCardNumber());

        cf.setGroupName(certType.getName());
        cf.setUsetId(certificate.getUsetId());
        cf.setState("有效");
        return cf;
    }

    @Override
    public BpCertificateListBean update(BpCertificateAddBean c) throws Exception {
        BpCertificate b = dao.getOne(c.getId());

        b.setCardNumber(c.getCardNumber());
        b.setContinuous(c.getContinuous());
        b.setHoldProject(c.getHoldProject());

        b.setRecheckDate(c.getRecheckDate());
        b.setEarlyDate(c.getEarlyDate());
        b.setValidityBegin(c.getValidityBegin());
        b.setValidityEnd(c.getValidityEnd());
        b.setHomeworkId(c.getHomeworkId());
        b.setImageId(c.getImage().getId());
        b.setPostpone(c.getPostpone());
        b.setDownloadurl(c.getImage().getDownloadUrl());
        b.setName(c.getImage().getName());

        BpCertificate certificate = dao.save(b);
        BpCertificateListBean cf = new BpCertificateListBean();
        cf.setId(certificate.getId());
        cf.setCardNumber(certificate.getCardNumber());
        cf.setGroupName(typeGroupItemDao.findNameById(b.getGroupId()));
        cf.setUsetId(certificate.getUsetId());
        cf.setState("有效");
        cf.setContent("暂时未知");
        return cf;
    }

    @Override
    public void delete(Long id) {
        dao.deleteById(id);
    }

    @Override
    public BpCertificateAddListBean getById(Long id) {
        BpCertificate c = dao.getOne(id);
        BpCertificateAddListBean b = new BpCertificateAddListBean();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        b.setUsetId(c.getUsetId());
        b.setGroupId(c.getGroupId());
        b.setCardNumber(c.getCardNumber());
        b.setContinuous(c.getContinuous());
        b.setHoldProject(c.getHoldProject());
        b.setHomeworkId(c.getHomeworkId());
        b.setPostpone(c.getPostpone());

        BaseAttachmentBean baseAttachmentBean = new BaseAttachmentBean();
        baseAttachmentBean.setDownloadUrl(c.getDownloadurl());
        baseAttachmentBean.setName(c.getName());
        baseAttachmentBean.setId(c.getImageId());
        b.setImage(baseAttachmentBean);
        if (c.getRecheckDate() != null) {
            b.setRecheckDate(sdf.format(c.getRecheckDate()));
        }
        if (c.getEarlyDate() != null) {
            b.setEarlyDate(sdf.format(c.getEarlyDate()));
        }
        if (c.getValidityBegin() != null) {
            b.setValidityBegin(sdf.format(c.getValidityBegin()));
        }
        if (c.getValidityBegin() != null) {
            b.setValidityBegin(sdf.format(c.getValidityBegin()));
        }
        if (c.getValidityEnd() != null) {
            b.setValidityEnd(sdf.format(c.getValidityEnd()));
        }

        BaseTypeGroupItem one = typeGroupItemDao.getOne(c.getGroupId());
        b.setGroupName(one.getName());
        return b;
    }

    @Override
    public String findCertNameByUserIdAndType(Long userId, String type) {
        List<String> list = dao.findCertNameByUserIdAndType(userId, type);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public List<BpCertificate> findCertNameByUserIdsAndType(List<Long> ids, String code) {
        return dao.findCertNameByUserIdsAndType(ids, code);
    }

}
